let ani1 = null;
let ani2 = null;
const api = require('../../utils/interface.js')
const math = require("../../utils/math.js")
const tools = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 1,
    rechargeLsit: [],
    consumptionLsit: [],
    cardId: '',
    state1:1,
    state2:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, "idddd")
    this.data.cardId = options.cardId
    this.getRechargeList()
    this.getMemberCardOrders()
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      isShow: false
    })
  },
  // 切换消费充值列表
  select(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex: index
    })
  },
  // 日期展开收起
  start(e) {
    console.log(e)
    let height = e.currentTarget.dataset.height
    let index = e.currentTarget.dataset.index
    console.log(e,"e")
    if (this.data.rechargeLsit[index].state) {
      ani1.height(0).step()
      ani2.rotate(0).step()
      this.data.rechargeLsit[index].animate = ani1.export()
      this.data.rechargeLsit[index].rotate = ani2.export()
      this.data.rechargeLsit[index].state = false
    } else {
      ani2.rotate(180).step()
      ani1.height(height + 'rpx').step()
      this.data.rechargeLsit[index].rotate = ani2.export()
      this.data.rechargeLsit[index].animate = ani1.export()
      this.data.rechargeLsit[index].state = true
    }
    this.setData({
      rechargeLsit: this.data.rechargeLsit
    })
  },
  startTo(e) {
    let height = e.currentTarget.dataset.height
    let index = e.currentTarget.dataset.index
    console.log(this.data.consumptionLsit, "consumptipn")
    if (this.data.consumptionLsit[index].state) {
      ani1.height(0).step()
      ani2.rotate(0).step()
      this.data.consumptionLsit[index].animate = ani1.export()
      this.data.consumptionLsit[index].rotate = ani2.export()
      this.data.consumptionLsit[index].state = false
    } else {
      ani2.rotate(180).step()
      ani1.height(height + 'rpx').step()
      this.data.consumptionLsit[index].rotate = ani2.export()
      this.data.consumptionLsit[index].animate = ani1.export()
      this.data.consumptionLsit[index].state = true
    }
    
    this.setData({
      consumptionLsit: this.data.consumptionLsit
    })
  },
  // 跳转订单详情
  go_consumptionOrderInfo(e) {
    let orderNumber = e.currentTarget.dataset.ordernumber
    wx.navigateTo({
      url: '/pages/myorder/success/success?orderNumber=' + orderNumber + "&tabIndex=" + 2,
    })
  },
  // 跳转订单详情
  go_rechargeOrderInfo(e) {
    console.log(e,"uss")
    let orderNumber = e.currentTarget.dataset.ordernumber
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/myorder/success/success?orderNumber=' + orderNumber + "&tabIndex=" + 1 + "&id="+id
    })
  },
  // 获取已支付订单
  getRechargeList() {
    let user = wx.getStorageSync('userInfo').id
    wx.showToast({ title: '加载中', icon: 'loading'});
    api.getMemberFastOrders({ memberId: '71176'}).then(res => {
      console.log(res.data.result,"521")
      if (res.data.result && res.data.result.length) {
        this.setData({
          state1: 2
        })
      } else {
        this.setData({
          state1: 3
        })
      }
      res.data.result.map(item => {
        item.animate = null
        item.rotate = null
        item.height = 169 * item.data.length
        item.state = true
        
        item.data.map(item => {
          if (item.productName == "LNG" || item.productName == "CNG") {
            item.productName = ""
          }
          item.amount_ = math.divide(item.amount, 100)
          item.date = tools.formatTime(new Date(item.createTime))
        })
      })
      this.setData({
        rechargeLsit: res.data.result
      })
      console.log(this.data.rechargeLsit)
      wx.hideToast();
    })
  },
  // 获取已退款订单
  getMemberCardOrders() {
    wx.showToast({ title: '加载中', icon: 'loading' });
    let user = wx.getStorageSync('userInfo').id
    api.getMemberRefundOrders({ page: 1, memberId: user, pageSize: 30 }).then(res => {
      if (res.data.result && res.data.result.length) {
        this.setData({
          state2: 2
        })
      } else {
        this.setData({
          state2: 3
        })
      }
      res.data.result.map(item => {
        item.animate = null
        item.rotate = null
        item.height = 169 * item.data.length
        item.state = true
       
        item.data.map(item => {
          if (item.productName == "LNG" || item.productName == "CNG") {
            item.productName = ""
          }
          item.amount_ = math.divide(item.amount, 100)
          item.date = tools.formatTime(new Date(item.createTime))
        })
      })
      this.setData({
        consumptionLsit: res.data.result
      })
      wx.hideToast();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    ani1 = wx.createAnimation({
      duration: 500,
      delay: 100,
      timingFunction: 'ease',
    })
    ani2 = wx.createAnimation({
      duration: 500,
      delay: 100,
      timingFunction: 'ease',
    })
  },
  onUnload() {
    console.log('返回按钮')
    wx.navigateBack({
      delta: 1
    })
  }


})